<?
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006, 2007 Russell Peter Phillips

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
?>

<script>
//Calculate character points spent and update span
function fnCalculate () {
	iCost = 0
	//Loop through all elements in the form
	for (i = 0; i < document.forms [0].length; i++) {
		//Elements that need to be counted have name prefixed with 'sk'
		if (document.forms [0].elements [i].name.slice (0,2) == 'sk') {
			//Only count checkboxes (if they are checked) & drop-downs
			if (document.forms [0].elements [i].checked || document.forms [0].elements [i].type == 'select-one'){
				iCost = iCost + parseInt (document.forms [0].elements [i].value)
			}
		}
	}
	//Maximum number of points to spend
	if (document.forms [0].elements ['chkNPC'].checked)
		<?
		echo "iMax = " . MAX_NPC_PTS . "\n";
		?>
	else
		<?
		echo "iMax = " . MAX_CHAR_PTS . "\n";
		?>
	//Display current cost
	if (iCost > iMax)
		document.getElementById ('spCost').innerHTML = '<span style = "color: red; font-weight: bold">Points used: ' + iCost + '</span>'
	else if (iCost == iMax)
		document.getElementById ('spCost').innerHTML = '<span style = "color: green; font-weight: bold">Points used: ' + iCost + '</span>'
	else
		document.getElementById ('spCost').innerHTML = 'Points used: ' + iCost
	//Return iCost so that function can be used for checking as well as updating spCost span
	return iCost
}

//Check for common errors on IC form
function ic_check () {
	fMain = document.forms [0]
	sMsg = ''
	sPoints = ''

	//Check literacy, numeracy, small weapons, points spent
	sMsgStart = "The following common skills have not been selected:\n\n"
	sMsg = ''
	if (!fMain.elements ['sk1'].checked)
		sMsg += "Small weapons (0)\n"
	if (!fMain.elements ['sk24'].checked)
		sMsg += "Literacy (1)\n"
	if (!fMain.elements ['sk26'].checked)
		sMsg += "Numeracy (0)\n"
	if (sMsg != '')
		sMsg = sMsgStart + sMsg
	if (fnCalculate () < iMax) {
		if (sMsg != '')
			sMsg += "\n"
		sMsg += "You have used fewer character points than you are allowed. Are you sure you want to do this?\n"
	}
	if (sMsg != '') {
		sMsg += "\nIf this is correct, click OK. If not, click Cancel to go back and make changes"
		return confirm (sMsg)
	}
	//If the script gets to here, then there are no errors. Just return true to submit form
	return true
}
</script>
